package cn.njxzc.book.masterexamples.ch14;

import java.util.*;

public class UpdateStu1 implements Comparable<Object> {
	String name;
	long id;

	public UpdateStu1(String name, long id) {
		this.id = id;
		this.name = name;
	}

	public int compareTo(Object o) {
		UpdateStu1 upstu = (UpdateStu1) o;
		int result = id > upstu.id ? 1 : (id == upstu.id ? 0 : -1);
		return result;
	}

	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public static void main(String[] args) {
		UpdateStu1 stu1 = new UpdateStu1("李同学", 01011);
		UpdateStu1 stu2 = new UpdateStu1("陈同学", 01021);
		UpdateStu1 stu3 = new UpdateStu1("王同学", 01051);
		UpdateStu1 stu4 = new UpdateStu1("马同学", 01012);
		TreeSet<UpdateStu1> tree = new TreeSet<>();
		tree.add(stu1);
		tree.add(stu2);
		tree.add(stu3);
		tree.add(stu4);
		Iterator<UpdateStu1> it = tree.iterator();
		System.out.println("Set集合中的所有元素：");
		while (it.hasNext()) {
			UpdateStu1 stu = (UpdateStu1) it.next();
			System.out.println(stu.getId() + " " + stu.getName());
		}
		it = tree.headSet(stu2).iterator();
		System.out.println("截取前面部分的集合：");
		while (it.hasNext()) {
			UpdateStu1 stu = (UpdateStu1) it.next();
			System.out.println(stu.getId() + " " + stu.getName());
		}
		it = tree.subSet(stu2, stu3).iterator();
		System.out.println("截取中间部分的集合");
		while (it.hasNext()) {
			UpdateStu1 stu = (UpdateStu1) it.next();
			System.out.println(stu.getId() + " " + stu.getName());
		}
	}
}
